import { ApiProperty } from '@nestjs/swagger'
import { IsBoolean, IsEnum, IsInt, IsOptional, IsString, Min, MinLength } from 'class-validator'

import { PagerDto } from '~/common/dto/pager.dto'
import { ConfigType } from '~/enums'
import { IsUnique } from '~/shared/database/constraints/unique.constraint'

import { ParamConfigEntity } from './param-config.entity'

export class ParamConfigSaveDto {
  @ApiProperty({ description: '参数配置ID' })
  @IsInt()
  @Min(0)
  @IsOptional()
  id?: number

  @ApiProperty({ description: '参数分组' })
  @IsString()
  @MinLength(1)
  category: string

  @ApiProperty({ description: '参数类型', enum: ConfigType })
  @IsEnum(ConfigType)
  type: ConfigType

  @ApiProperty({ description: '参数名称' })
  @IsString()
  @MinLength(1)
  name: string

  @ApiProperty({ description: '参数键名' })
  @IsUnique({ entity: ParamConfigEntity, message: '该键名已存在' })
  @IsString()
  @MinLength(3)
  configKey: string

  @ApiProperty({ description: '参数值' })
  @IsString()
  value: string

  @ApiProperty({ description: '是否可见' })
  @IsBoolean()
  visible: boolean

  @ApiProperty({ description: '备注' })
  @IsOptional()
  @IsString()
  remark?: string
}

export class ParamConfigQueryDto extends PagerDto {
  @ApiProperty({ description: '参数分组' })
  @IsString()
  @IsOptional()
  category?: string

  @ApiProperty({ description: '参数类型' })
  @IsEnum(ConfigType)
  @IsOptional()
  type?: ConfigType

  @ApiProperty({ description: '参数名称' })
  @IsString()
  @IsOptional()
  name?: string

  @ApiProperty({ description: '参数键名' })
  @IsString()
  @IsOptional()
  configKey?: string
}
